package com.itheima.web.controller.store.questionitem;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.QuestionItem;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "QuestionItemServlet", value = "/store/questionItem")
public class QuestionItemServlet extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            this.list(request, response);
        } else if ("save".equals(operation)) {
            this.save(request, response);
        }else if("toEdit".equals(operation)){
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
            this.edit(request, response);
        }else if ("delete".equals(operation)) {
            this.delete(request, response);
        }
    }


    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class);
        questionItemService.delete(questionItem);
        list(request,response);
    }


    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class, "yyyy-MM-dd");
        //QuestionItemService questionItemService= new QuestionItemServiceImpl();
        questionItemService.update(questionItem);
        list(request,response);
    }


    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        QuestionItem questionItem = questionItemService.findById(id);
        request.setAttribute("questionItem",questionItem);
        request.setAttribute("operation","edit");
        list(request,response);
    }


    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //QuestionItemService questionItemService = new QuestionItemServiceImpl();
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class, "yyyy-MM-dd");
        questionItemService.save(questionItem);
        list(request,response);
    }





    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //QuestionItemService questionItemService = new QuestionItemServiceImpl();

        String questionId = request.getParameter("questionId");
        request.setAttribute("questionId",questionId);
        PageInfo all = questionItemService.findAll(questionId, 1, 100);
        request.setAttribute("page",all);
        if (request.getAttribute("operation")==null){
            request.setAttribute("operation","save");
        }
        request.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(request, response);
    }
}

